package com.guaoran.newsdata.provider.service;

import com.guaoran.newsdata.provider.entity.News;
import com.guaoran.newsdata.provider.repository.NewsRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Optional;

/**
 * @author : gucheng
 * @Description : <br/>
 * @date :2018/12/9 12:23
 */
@Service
public class NewsService {
    @Autowired
    private NewsRepository repository;

    /**
     * 保存对象
     * @param news
     */
    public void save(News news){
        repository.save(news);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Long id){
        repository.deleteById(id);
    }

    /**
     * 分页查询
     * @param pageable
     * @return
     */
    public Page<News> findListByPage(Pageable pageable){
        return repository.findAll(pageable);
    }

    public News findNewsById(Long id){
        News news = new News();
        Optional<News> optionalNews = repository.findById(id);
        BeanUtils.copyProperties(optionalNews.get(),news);
        return news;
    }
    /**
     * 查询所有
     * @return
     */
    public Collection<News> findAllNews(){
        return (Collection<News>) repository.findAll();
    }
}
